﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using JEdwonTune.Lib;
using JEdwonTune.Common;

namespace JEdwonTune.Crud {
    public partial class FrmEditModel : FrmXmlNodeEditorBase {
        public FrmEditModel() {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e) {
            if (String.IsNullOrEmpty(this.txtName.Text.Trim())) {
                MessageBox.Show("请输入Name");
                return;
            }
            if (String.IsNullOrEmpty(this.txtType.Text.Trim())) {
                MessageBox.Show("请输入Type");
                return;
            }
            Int32 pagesize = 0;
            if (!String.IsNullOrEmpty(this.txtPageSize.Text.Trim())) {
                try {
                    pagesize = Convert.ToInt32(this.txtPageSize.Text);
                    if (pagesize < 0) pagesize = 0;
                } catch {
                    MessageBox.Show("PageSize请输入一个大于0的整数值");
                    return;
                }
            }
            SetNodeAttrValue(base.Node, "name", this.txtName.Text.Trim());
            SetNodeAttrValue(base.Node, "type", this.txtType.Text.Trim());
            SetNodeAttrValue(base.Node, "pagesize", pagesize.ToString());
            SetNodeAttrValue(base.Node, "create", (this.cbCreate.Checked).ToString().ToLower());
            SetNodeAttrValue(base.Node, "update", (this.cbUpdate.Checked).ToString().ToLower());
            SetNodeAttrValue(base.Node, "read", (this.cbRead.Checked).ToString().ToLower());
            SetNodeAttrValue(base.Node, "delete", (this.cbDelete.Checked).ToString().ToLower());
            SetNodeAttrValue(base.Node, "order", (this.cbOrder.Checked).ToString().ToLower());
            this.DialogResult = DialogResult.OK;
            this.Close();
        }


        private void FrmEditModel_Load(object sender, EventArgs e) {
            if (base.Node == null) {
                return;
            }
            if (base.Node.Name != "model") {
                MessageBox.Show("编辑的节点不是model节点");
                this.Close();
                return;
            }
            if (base.Node.Attributes["name"] != null) {
                this.txtName.Text = base.Node.Attributes["name"].Value;
            }
            if (base.Node.Attributes["type"] != null) {
                this.txtType.Text = base.Node.Attributes["type"].Value;
            }
            if (base.Node.Attributes["pagesize"] != null) {
                this.txtPageSize.Text = base.Node.Attributes["pagesize"].Value;
            }

            this.cbCreate.Checked = XmlHelper.ReadBooleanAttr(base.Node, "create", true);
            this.cbDelete.Checked = XmlHelper.ReadBooleanAttr(base.Node, "delete", true);
            this.cbOrder.Checked = XmlHelper.ReadBooleanAttr(base.Node, "order", false);
            this.cbRead.Checked = XmlHelper.ReadBooleanAttr(base.Node, "read", true);
            this.cbUpdate.Checked = XmlHelper.ReadBooleanAttr(base.Node, "update", true);
        }

        private void button2_Click(object sender, EventArgs e) {
            this.Close();
        }
    }
}
